<?php

namespace app\comm\service;
use app\mall\model\Goods;
use app\comm\model\GoodsComment;
use app\comm\model\GoodsMaterial;
use app\comm\model\GoodsYongliao;
use app\comm\model\User;
use app\comm\model\UserPointlog;
use app\comm\model\UserWalletlog;

use ServS\DbShowcollo;
use app\comm\service\SysToolsLevePoint;
/**
 * 商品服务层
 * 
 */
class AdminUserService{	
	/**
	 * 用户列表，后台用户列表
	 */
	public function getuserlist($urlcode,$get=''){
		$user = User::where('moblie','like','%'.$get.'%' )->paginate(10, false, ['query' =>$urlcode]);
		// foreach ($user as $key => &$value) {
		// 	$value->
		// }
		return $user;
	}

	/**
	 * 获取用户信息
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function getuser($id){
		$user = User::get($id);
		return $user->toArray();
	}

	/**
	 * 保存用户信息
	 */
	public function updateuser($post){
		$data = $post;
		$user = new User;
		if($post['asdesmogey']!='0'){
			if($post['asdes']==1){
				$rsul = $this->pointcd($post['uid'],1,$post['asdesmogey']);
			}
			if($post['asdes']==2){
				$rsul= $this->pointcd($post['uid'],0,$post['asdesmogey']);
			}
			if($rsul==3){
				return 3;
			}
		}
		unset($data['asdesmogey']);unset($data['asdes']);
		$result = DbShowcollo::refresh($user,'uid',$post['uid'],$data);
		return $result;
	}

	/**
	 * 余额变更
	 * @return [type] [description]
	 */
	public function asdesmoney($post){
		$user = new User;
		$data =['uid'      => $post['uid'],      'store_id'=>'',
				'order_id' => '', 'order_sn'=>'',	'uname'=>'',
				'amount'=>$post['asdesmoney']];
		if($post['asdes'] ==1 ){
			$use = User::where('uid', $post['uid'])->setInc('user_money', $post['asdesmoney']);
			$use = User::where('uid', $post['uid'])->setInc('level_point', $post['asdesmoney']);
			$ingo = $this->getuser($post['uid']);
			$data['chargetype'] = 2;
			$data['charge_cn'] = '后台增加';
			$data['sign']=1;
			$this->pointcrde($post['uid'],$post['asdesmoney'],1);
		}
		if($post['asdes']==2){
			$ingo = $this->getuser($post['uid']);
			if($ingo['user_money']<$post['asdesmoney']){
				return 3;
			}
			$use = User::where('uid', $post['uid'])->setDec('user_money', $post['asdesmoney']);
			$ingo = $this->getuser($post['uid']);
			$data['chargetype'] = 1;
			$data['charge_cn'] = '后台减少';
			$data['sign']=1;
		}
		$data['cur_amount'] = $ingo['user_money'];
		$res = DbShowcollo::add(new UserWalletlog,$data);
		return $res;
	}	

	/**
	 * 积分变更操作,金钱变换
	 */
	public function  pointcrde($uid,$money,$type,$type_cn='',$whname=''){
		 $adminUserService = 	new SysToolsLevePoint;
		 $syspoint        = 	new UserPointlog;
	 	 $list = $adminUserService->jifenjisuan($money);
	 	 $use = User::where('uid', $uid)->setInc('pay_points', $list['jifen']);		   //erdao  原先的
	 	 $use = User::where('uid', $uid)->setInc('level_point', $list['jifen']);		//erdao  原先的
	 	 // $money = floor($money);					//向下取整
	 	 // $use = User::where('uid', $uid)->setInc('pay_points', $money);
	 	 // $use = User::where('uid', $uid)->setInc('level_point', $money);
	 	 $ingo = $this->getuser($uid);
	 	 $data = ['uid'   =>$uid,           'store_id'=>'','uidtype'=>'2','type'=>$type,'signid' =>$list['id'],
	 			  'point' =>$list['jifen'], 'curpoint'=>$ingo['pay_points'],'type_cn'=>$list['jifen_cn'],'whname'=>$whname];
	 	
	 	 $res = DbShowcollo::add($syspoint,$data); 
	 	 
	 	 return  $res;
	}

	/**
	 * 后台直接兑积分进行变更
	 */
	public function pointcd($uid,$type,$point){
		 
		 $adminUserService = 	new SysToolsLevePoint;
		
		 $syspoint         = 	new UserPointlog;
		
		 $adminUserService = 	new SysToolsLevePoint;
		 if($type==1){
		 	 $use  = User::where('uid', $uid)->setInc('pay_points',$point);
		 	 $list = $adminUserService->getpointbyid(11);
		 	 $typecn =1;
		 }
		 if($type==0){
		 	 if($this->checkpoint($uid,$point))return 3;
		 	 $use = User::where('uid', $uid)->setDec('pay_points',$point);
		 	 $typecn = 0;
		 	 $list = $adminUserService->getpointbyid(11);
		 }
		
		 $ingo = $this->getuser($uid);
		 $data=['uid'=>$uid,'store_id'=>'','uidtype'=>'','type'=>$typecn,'signid' =>$list['id'],
	 			'point'=>$point, 'curpoint'=>$ingo['pay_points'],'type_cn'=>$list['remark'],'whname'=>$list['remark']];
	 			 $res = DbShowcollo::add($syspoint,$data); 
	 	 return  $res;
	}

	/**
	 * 检查积分时候够用
	 */
	public function checkpoint($uid,$point){
		 $ingo = $this->getuser($uid);
		 if($ingo['pay_points']>$point){
		 	return  false;
		 }
		 return true;
	}
}